package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.core.graphics.drawable.toBitmap

class MainActivity2 : ComponentActivity() {

    private val REQUEST_IMAGE_CAPTURE = 1
    private lateinit var sharedPreferences: SharedPreferences

    @SuppressLint("WrongViewCast")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)

        sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)

        val takePictureButton: Button = findViewById(R.id.button4)
        val editText1 = findViewById<EditText>(R.id.editTextText)
        val editText2 = findViewById<EditText>(R.id.editTextText2)
        val editText3 = findViewById<EditText>(R.id.editTextText3)
        val editText4 = findViewById<EditText>(R.id.editTextText4)
        val radioButton1 = findViewById<RadioButton>(R.id.radioButton4)
        val radioButton2 = findViewById<RadioButton>(R.id.radioButton5)
        val checkBox1 = findViewById<CheckBox>(R.id.checkBox6)
        val checkBox2 = findViewById<CheckBox>(R.id.checkBox7)
        val checkBox3 = findViewById<CheckBox>(R.id.checkBox8)
        val checkBox4 = findViewById<CheckBox>(R.id.checkBox9)
        val button2 = findViewById<Button>(R.id.button5)
        val button3 = findViewById<Button>(R.id.button6)
        val imageView = findViewById<ImageView>(R.id.imageView5)

        // 从SharedPreferences中读取已保存的信息
        editText1.setText(sharedPreferences.getString("inputText1", ""))
        editText2.setText(sharedPreferences.getString("inputText2", ""))
        editText3.setText(sharedPreferences.getString("inputText3", ""))
        editText4.setText(sharedPreferences.getString("inputText4", ""))

        val selectedCheckboxes = sharedPreferences.getStringSet("selected_checkboxes", setOf()) ?: setOf()
        checkBox1.isChecked = selectedCheckboxes.contains(checkBox1.text.toString())
        checkBox2.isChecked = selectedCheckboxes.contains(checkBox2.text.toString())
        checkBox3.isChecked = selectedCheckboxes.contains(checkBox3.text.toString())
        checkBox4.isChecked = selectedCheckboxes.contains(checkBox4.text.toString())

        val selectedOption = sharedPreferences.getString("selected_option", "")
        if (selectedOption == radioButton1.text) {
            radioButton1.isChecked = true
        } else if (selectedOption == radioButton2.text) {
            radioButton2.isChecked = true
        }

        // 点击按钮开始拍照
        takePictureButton.setOnClickListener {
            dispatchTakePictureIntent()
        }

        // 保存按钮的事件
        button2.setOnClickListener {
            // 处理输入文本
            val inputText1 = editText1.text.toString()
            val inputText2 = editText2.text.toString()
            val inputText3 = editText3.text.toString()
            val inputText4 = editText4.text.toString()

            // 保存信息到SharedPreferences
            with(sharedPreferences.edit()) {
                putString("inputText1", inputText1)
                putString("inputText2", inputText2)
                putString("inputText3", inputText3)
                putString("inputText4", inputText4)

                // 处理复选框
                val selectedCheckboxes = mutableSetOf<String>()
                if (checkBox1.isChecked) selectedCheckboxes.add(checkBox1.text.toString())
                if (checkBox2.isChecked) selectedCheckboxes.add(checkBox2.text.toString())
                if (checkBox3.isChecked) selectedCheckboxes.add(checkBox3.text.toString())
                if (checkBox4.isChecked) selectedCheckboxes.add(checkBox4.text.toString())
                putStringSet("selected_checkboxes", selectedCheckboxes)

                // 处理单选框
                if (radioButton1.isChecked) {
                    putString("selected_option", radioButton1.text.toString())
                } else if (radioButton2.isChecked) {
                    putString("selected_option", radioButton2.text.toString())
                }

                apply()
            }

            //图片处理
            val imageBitmap = imageView.drawable?.toBitmap()
            if (imageBitmap != null) {
                 intent.putExtra("photo_bitmap", imageBitmap)
            }

            // 跳转到MainActivity1
            val intent = Intent(this, MainActivity1::class.java)
            startActivity(intent)
        }

        // 取消按钮的事件
        button3.setOnClickListener {
            finish()
        }
    }

    private fun dispatchTakePictureIntent()
    {
        val takePictureIntent=Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (intent.resolveActivity(packageManager) !=null){
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        }
    }

    override fun onActivityResult(requestCode:Int,resultCode:Int,data:Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val imageview = findViewById<ImageView>(R.id.imageView5)
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
            val imageBitmap: Bitmap = data?.extras?.get("data") as Bitmap
            imageview.setImageBitmap(imageBitmap)
        }
    }
}